//
//  CategoryViewController.m
//  futureLive
//
//  Created by zhoubiwen on 15/11/17.
//  Copyright © 2015年 ZHOU_LI_SUN. All rights reserved.
//

#import "CategoryViewController.h"
#import "CategoryCollectionViewCell.h"
#import "NetWorkManager.h"
#import "CategoryModel.h"
#import "CategoryDownViewCell.h"

@interface CategoryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIView *upView;


@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *sourceArray;

@property (nonatomic, strong) NSMutableArray *Arr;

@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建布局类
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    flowLayout.itemSize                    = CGSizeMake(self.upView.bounds.size.width - self.upView.bounds.size.width / 1.3, 150);
    //最小的行间距
    flowLayout.minimumLineSpacing          = 10;
    //item的间距
    flowLayout.minimumInteritemSpacing     = 5;

    //创建collectionView
    UICollectionView *collectionView       = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:flowLayout];
    //设置数据源和代理
    collectionView.dataSource              = self;
    collectionView.delegate                = self;
    collectionView.backgroundColor         = [UIColor clearColor];

    self.collectionView                    = collectionView;
    [self.upView addSubview:collectionView];

    [self initData];
    [self initDownData];
    
    [collectionView registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [collectionView registerClass:[CategoryDownViewCell class] forCellWithReuseIdentifier:@"downcell"];

    
    
    
}
-(void)initDownData{
    [NetWorkManager getJsonDataWithURL:categoryURL success:^(id data) {
        self.Arr                  = [NSMutableArray new];
        NSDictionary *dataDict    = data[@"data"];
        NSDictionary *aditemsDict = dataDict[@"aditems"];
        NSMutableArray *listArr   = aditemsDict[@"list"];
        
        for (NSDictionary *listDict in listArr) {
            CategoryModel *model = [[CategoryModel alloc] init];
            [model setValuesForKeysWithDictionary:listDict];
            [self.Arr addObject:model];
            
        }
        [self.collectionView reloadData];
        
    } fail:^{
        NSLog(@"网络数据请求失败.");
        
    }];
    
}
-(void)initData{
    
    [NetWorkManager getJsonDataWithURL:categoryURL success:^(id data) {
        self.sourceArray         = [NSMutableArray new];
        NSDictionary *dataDict   = data[@"data"];
        NSMutableArray *itemsArr = dataDict[@"items"];
        
        for (NSDictionary *itemDict in itemsArr) {
            CategoryModel *model = [[CategoryModel alloc] init];
            [model setValuesForKeysWithDictionary:itemDict];
            [self.sourceArray addObject:model];
        }
        [self.collectionView reloadData];
    } fail:^{
        NSLog(@"网络数据请求失败.");
    }];
    
    
}

#pragma mark------collectionView
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.sourceArray.count;
        
    }
    
    return self.Arr.count;
    
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        CategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        
        CategoryModel *model = self.sourceArray[indexPath.item];
        cell.categoryModel = model;
        return cell;
    }else if(indexPath.section == 1){
        
        CategoryDownViewCell *downcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"downcell" forIndexPath:indexPath];
        
        CategoryModel *model = self.Arr[indexPath.item];
        downcell.categoryModel = model;
        
        return downcell;
        
        
        
    }
    
    
    
    return nil;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
